{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b9a014e3",
   "metadata": {},
   "source": [
    "# 定义人脸识别的函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23aed509",
   "metadata": {},
   "source": [
    "## 人脸检测的定义函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2bc1c879",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = '4udXjI9wvOnmb6uKVqr7gS0iwvSQcmc6'\n",
    "API_sercret = 'FtFY8-LLw7rHqVd6NEsUviJ7PJE5ASJg'\n",
    "def face_detect(API_key,API_sercret,image_path,landmark=None,attributes=None,face_rectangle=None,beauty_score_min=0,beauty_score_max=100):\n",
    "    \"\"\"该函数为调用face++ face_detect接口,用于人脸检测\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    files = {'image_file': (image_path, open(image_path, 'rb'), 'multipart/form-data')}\n",
    "    payload = {\n",
    "            'api_key':API_key,\n",
    "            'api_secret':API_sercret,\n",
    "            'image_url':image_url,\n",
    "            \"return_landmark\":landmark,\n",
    "            \"return_attributes\":attributes,\n",
    "            \"face_rectangle\":face_rectangle,\n",
    "            \"beauty_score_min\":beauty_score_min,\n",
    "            \"beauty_score_max\":beauty_score_max\n",
    "            \n",
    "            }\n",
    "    \n",
    "\n",
    "    r = requests.post(base_url, params = payload,files = files )\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c3d23be",
   "metadata": {},
   "source": [
    "## 人脸分析的定义函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "79692ede",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = '4udXjI9wvOnmb6uKVqr7gS0iwvSQcmc6'\n",
    "API_sercret = 'FtFY8-LLw7rHqVd6NEsUviJ7PJE5ASJg'\n",
    "def face_analyze(API_Key,API_Secret,face_tokens,attributes):\n",
    "    \"\"\"该函数为调用face++ face_analyze接口，用于人脸分析\"\"\"\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "    payload = {\n",
    "        \"api_key\":API_Key,\n",
    "        \"api_secret\":API_Secret,\n",
    "        \"face_tokens\":face_tokens,\n",
    "        \"return_attributes\":attributes\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d4c865a",
   "metadata": {},
   "source": [
    "## 人脸对比的定义函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "fe47450b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = '4udXjI9wvOnmb6uKVqr7gS0iwvSQcmc6'\n",
    "API_sercret = 'FtFY8-LLw7rHqVd6NEsUviJ7PJE5ASJg'\n",
    "def face_compare(API_Key,API_secret,face_token1,face_token2):\n",
    "    \"\"\"该函数为调用face++ face_compare接口，用于人脸对比\"\"\"\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "    payload = {\n",
    "        \"api_key\":API_Key,\n",
    "        \"api_secret\":API_secret,\n",
    "        \"face_token1\":face_token1,\n",
    "        \"face_token2\":face_token2\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f3195ce",
   "metadata": {},
   "source": [
    "## 人脸搜索的定义函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "084de436",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = '4udXjI9wvOnmb6uKVqr7gS0iwvSQcmc6'\n",
    "API_sercret = 'FtFY8-LLw7rHqVd6NEsUviJ7PJE5ASJg'\n",
    "def face_search(API_Key,API_Secret,face_token,faceset_token):\n",
    "    \"\"\"该函数为调用face++ face_search接口，用于人脸查找\"\"\"\n",
    "    url = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "    payload = {\n",
    "        \"api_key\":API_Key,\n",
    "        \"api_secret\":API_Secret,\n",
    "        \"face_token\":face_token,\n",
    "        \"faceset_token\":faceset_token\n",
    "    }\n",
    "    r = requests.post(url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "303ccaf2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function face_detect in module __main__:\n",
      "\n",
      "face_detect(API_key, API_sercret, image_path, landmark=None, attributes=None, face_rectangle=None, beauty_score_min=0, beauty_score_max=100)\n",
      "    该函数为调用face++ face_detect接口,用于人脸检测\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(face_detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "334e464b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function face_analyze in module __main__:\n",
      "\n",
      "face_analyze(API_Key, API_Secret, face_tokens, attributes)\n",
      "    该函数为调用face++ face_analyze接口，用于人脸分析\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(face_analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "5d9a155e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function face_compare in module __main__:\n",
      "\n",
      "face_compare(API_Key, API_secret, face_token1, face_token2)\n",
      "    该函数为调用face++ face_compare接口，用于人脸对比\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(face_compare)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "eaf98031",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function face_search in module __main__:\n",
      "\n",
      "face_search(API_Key, API_Secret, face_token, faceset_token)\n",
      "    该函数为调用face++ face_search接口，用于人脸查找\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(face_search)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35f18e2b",
   "metadata": {},
   "source": [
    "## 先尝试创建人脸库，以解决宿舍门禁系统问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "3ae08fc3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建人脸库\n",
    "import requests\n",
    "API_key = '4udXjI9wvOnmb6uKVqr7gS0iwvSQcmc6'\n",
    "API_sercret = 'FtFY8-LLw7rHqVd6NEsUviJ7PJE5ASJg'\n",
    "FaceSet_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_sercret,\n",
    "}\n",
    "r = requests.post(url = FaceSet_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "1cf5df6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "d4382566",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '101f801998c405c268f6ba541c4d9258',\n",
       " 'time_used': 168,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648544271,76daf223-6369-46a0-9730-8f8589a56b48',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "14626282",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'101f801998c405c268f6ba541c4d9258'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token = r.json()['faceset_token']\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "371c62d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查询人脸库的信息\n",
    "url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail'  \n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'faceset_token':'9339e8a57f42445305f988939bbaebef'\n",
    "}\n",
    "r = requests.post(url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "ab7a0185",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "c32edb2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '9339e8a57f42445305f988939bbaebef',\n",
       " 'tags': '',\n",
       " 'time_used': 109,\n",
       " 'user_data': '',\n",
       " 'display_name': '',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1648544273,9ae96ac7-7a88-4c87-9c15-be6ef3afd349',\n",
       " 'outer_id': ''}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "a8e906d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 更新人脸库\n",
    "url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/update'\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'faceset_token':faceset_token,\n",
    "    'new_outer_id': 'API',\n",
    "    'display_name':'宿舍比对',\n",
    "    'user_data':'4人',\n",
    "    'tags':''\n",
    "}\n",
    "r = requests.post(url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "eebb313a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "5d17bdc9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '101f801998c405c268f6ba541c4d9258',\n",
       " 'request_id': '1648544306,9b828748-8d37-4c27-ba92-b07cb01b79bb',\n",
       " 'time_used': 81,\n",
       " 'outer_id': 'API'}"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "088a94d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取人脸库的信息\n",
    "url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "}\n",
    "r = requests.post(url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "ab9a043c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "630bf1e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Response.json of <Response [200]>>"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b37c78d7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
